import { getUserInfo, clearCacheAll } from '../../utils/auth'
import http from '../../utils/http'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null,

        showPicker: false,
        showMobile: false,
        showPwd: false,
        showNickName: false,
        canSendSms: true,

        columns: ['男', '女', '保密'],

        time: 10000

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({ userInfo: getUserInfo() });
    },

    /**
     * 修改性别
     */
    changeSexPop() {
        this.setData({ showPicker: true })
    },
    onConfirm(event) {
        const { picker, value, index } = event.detail;
        console.log(`当前值：${value}, 当前索引：${index}`);
    },
    onCancel() {
        this.setData({ showPicker: false })
    },

    /**
     * 更换手机号
     */
    showModifyMobile() {
        this.setData({ showMobile: true })
    },
    onConfirmMobile(event) {
        const { picker, value, index } = event.detail;
        console.log(`当前值：${value}, 当前索引：${index}`);
    },
    onCancelMobile() {
        this.setData({ showMobile: false, canSendSms: true })
    },

    /**
     * 修改密码
     */
    showModifyPwd() {
        this.setData({ showPwd: true })
    },
    onConfirmPwd(event) {
        const { picker, value, index } = event.detail;
        console.log(`当前值：${value}, 当前索引：${index}`);
    },
    onCancelPwd() {
        this.setData({ showPwd: false, canSendSms: true })
    },
    
    /**
     * 倒计时结束触发
     */
    countDownFinished() {
        this.setData({ canSendSms: true })
    },

    // 发送短信
    async $sendSms(e) {
        let { type } = e.currentTarget.dataset
        let { canSendSms } = this.data

        if (!canSendSms) return

        let res = await http.request('http://likeshopv2.yixiangonline.com/api/order/lists?page_no=1&type='+type, {}, {
            headers: {
                token: 'd74120e227ab2c0ee5744d77a96991af'
            }
        })
        console.log(res);

        if(res.status || res.code == 1) {
            // 开始倒计时
            this.setData({ canSendSms: false })
            this.selectComponent('.control-count-down-modify-' + type).start()
        } else {

        }
    },

    logout() {
        wx.showModal({
            title: '提示',
            content: '确定要退出登录？',
            showCancel: false,
            success(res) {
                if (res.confirm) {
                    clearCacheAll()
                    wx.switchTab({
                        url: '/pages/index/index'
                    })
                }
            }
        })
    }

})